import pandas as pd

pd.set_option('display.unicode.east_asian_width', True)

df = pd.DataFrame({'班级':['一班', '一班', '一班', '二班', '二班', '二班'],
                   '姓名':['刘武', '王振', '赵胜', '赵霞', '方芳', '齐婷'], '语文':[85,102,96,126,130,135], '数学':[100,90,124,123,140,109], '英语':[83,110,123,103,135,90]})
print('原始数据:\n', df)

group1 = df.groupby('班级')
print('以班级列按行分组:')
for i, group in group1:
    print(i)
    print(group)
print('分组后一班的数据：\n', group1.get_group('一班'))


group2 = df.groupby({'语文':'总成绩','数学':'总成绩','英语':'总成绩'}, axis=1)
print('以列标签按列分组：')
for i in group2:
    print(i)
df['总成绩'] = group2.agg('sum')
print('添加总成绩后的数据:\n', df)